package actions;

import java.util.Collections;
import java.util.HashMap;

import objects.MessageCodes;
import objects.cluster.ClusterInfo;
import objects.shared.Message;
import objects.user.UserLoginData;
import objects.user.RegisterData;
import objects.user.UserInfo;
import ssh.SSHUtils;
import utils.MessageStrings;
import database.Database;

public class ClientFunctions implements MessageCodes  {
	
	public static Message registerUser(RegisterData ri) throws Exception
	{
		UserInfo ui;
		if((ui = Database.users.get(ri.user)) != null) {
			return new Message(ERR,MessageStrings.usernameInUse);
		}
		//TODO add email check from SQL
		if(!SSHUtils.RegisterUser(ri.user, ri.pass)) {
			return new Message(ERR,MessageStrings.fileserverError);
		}
		ui = new UserInfo(ri);
		Database.users.put(ri.user, ui);
		if(Database.registerUser(ui) == SQL_ERR) {
			return new Message(ERR,MessageStrings.sqlError);
		}
		return new Message(OK);
		
	}
	
	public static Object userLogin(UserLoginData li)
	{
		String uname = li.user;
		String passwd = li.pass;
		UserInfo ui;
		if((ui = Database.users.get(uname)) == null || !ui.getPasswd().equals(passwd) )
		{
			return new Message(ERR, MessageStrings.invalidCred);
		}
		if(ui.isConnected)
		{
			return new Message(ERR, MessageStrings.userAlreadyConnected);
		}
		return ui;
	}
}
